package cn.fl.system.service.impl;

import cmm.comm.facade.framework.api.ISysCdMapApiService;
import cmm.comm.facade.framework.api.ISysCtrlCfgApiService;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.condition.SysCdMapDQueryCondition;
import cmm.comm.facade.framework.condition.SysCtrlCfgQueryCondition;
import cmm.comm.facade.framework.dto.SysCdMapDDTO;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.comm.facade.smscode.api.ICmmSmsCdApiService;
import cmm.comm.facade.syssms.api.ISmsAddApiService;
import cmm.comm.facade.syssms.dto.SmsSendParamDTO;
import cmm.mid.core.framework.dto.BaseParamDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.multi.redis.service.IMultiRedisService;
import cmm.user.facade.system.api.*;
import cmm.user.facade.system.condition.SysUsrDatAuthRQueryCondition;
import cmm.user.facade.system.condition.SysUsrFunAuthRQueryCondition;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.constant.SysResPowerConstant;
import cmm.user.facade.system.constant.SysUsrConstant;
import cmm.user.facade.system.dto.*;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.EncryptUtil;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.expatriate.api.IExpBizManRecApiService;
import cn.fl.expatriate.condition.ExpBizManRecQC;
import cn.fl.expatriate.dto.ExpBizManRecDTO;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.login.loginEnum.LoginType;
import cn.fl.login.token.EasyTypeToken;
import cn.fl.system.service.ISysUsrFunAuthRService;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.transform.SysUsrVOTransform;
import cn.fl.system.vo.SysUsrVO;
import cn.lionbridge.center.dto.UserDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.framework.enums.common.MagicNumReplaceEnum;
import fl.spl.facade.spl.api.*;
import fl.spl.facade.spl.condition.SplInfoQueryCondition;
import fl.spl.facade.spl.condition.SplUsrRQC;
import fl.spl.facade.spl.dto.SplInfoDTO;
import fl.spl.facade.spl.dto.SplUsrRDTO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 系统用户服务实现
 */
@Service
public class SysUsrServiceImpl implements ISysUsrService {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(SysUsrServiceImpl.class);

    /**
     * 系统用户 API 暴露服务
     */
    @Autowired
    private ISysUsrApiService sysUsrApiService;

    /**
     * 本地数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @Autowired
    private ISplInfoApiService splInfoApiService;

    @Autowired
    private ISplInfoQueryApiService splInfoQueryApiService;

    @Autowired
    private ISplInfoModifyApiService splInfoMofidyApiService;

    @Autowired
    private ISplInfoRemoveApiService splInfoRemoveApiService;

    @Autowired
    private ISysUsrQueryApiService sysUsrQueryApiService;

    @Autowired
    private ISysUsrModifyApiService sysUsrModifyApiService;

    @Autowired
    private ICmmSmsCdApiService cmmSmsCdApiService;

    @Autowired
    private ISmsAddApiService smsAddApiService;

    @Autowired
    private ISysResPowerApiService sysResPowerApiService;

    @Autowired
    private ISysCtrlCfgApiService sysCtrlCfgApiService;

    @Autowired
    private ISplUsrRApiService splUsrRApiService;

    @Autowired
    private ISysUsrAppAddApiService sysUsrAppAddApiService;

    @Resource(name = "userRedisService")
    private IMultiRedisService userRedisService;

    @Autowired
    private ISysCdMapApiService sysCdMapApiService;

    @Autowired
    private ISysOrgApiService sysOrgApiService;

    @Autowired
    private ISysUsrFunAuthRService sysUsrFunAuthRService;

    @Autowired
    private IPriorityApiService priorityApiService;

    @Autowired
    private IExpBizManRecApiService expBizManRecApiService;

    @Override
    public SysUsrVO selectById(Long id, String sysCd) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        SysUsrDTO dto = this.sysUsrApiService.selectSysUsrById(id, sysCd, CurrentThreadContext.getCurrentOrgCd());

        SysUsrVO sysUsrVO = SysUsrVOTransform.toVO(dto);
        if (null != sysUsrVO) {
            //如果是供应商用户类型,需要获取供应商关系数据
            String usrTypCd = dto.getUsrTypCd();
            String usrTypCdDic = PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_SYS_USR_TYP_CD_SPL);
            if (usrTypCdDic.equals(usrTypCd)) {
                SplUsrRQC splUsrRQC = new SplUsrRQC();
                splUsrRQC.setUsrTypCd(usrTypCd);
                splUsrRQC.setUsrId(dto.getId());

                List<SplUsrRDTO> list = null;
                try {
                    list = this.splInfoQueryApiService.searchSplUsrRList(splUsrRQC);
                } catch (Exception e) {
                    BaseLogUtils.error(logger, "selectById", "获取供应商用户关系失败!" + JSON.toJSONString(splUsrRQC), e);
                }
                if (CollectionUtils.isNotEmpty(list)) {
                    SplUsrRDTO splUsrRDTO = list.get(0);
                    Long splId = splUsrRDTO.getSplId();
                    Integer isChg = splUsrRDTO.getIsChg();
                    String splNm = splUsrRDTO.getSplNm();
                    sysUsrVO.setSplId(splId);
                    sysUsrVO.setIsSplChg(isChg);
                    sysUsrVO.setSplNm(splNm);

                    //把所有的供应商加入到一个集合中前台显示
                    JSONArray splInfoArray = new JSONArray();
                    JSONObject splInfoObj = null;
                    for (SplUsrRDTO usrRDto : list) {
                        splInfoObj = new JSONObject();
                        Long splIdTem = usrRDto.getSplId();
                        splInfoObj.put("id", splIdTem);
                        splInfoObj.put("name", usrRDto.getSplNm());
                        splInfoObj.put("isCheck", false);
                        if(null != splId){
                            splInfoObj.put("isCheck", splId.equals(splIdTem) ? true : false);
                        }
                        splInfoArray.add(splInfoObj);
                    }

                    sysUsrVO.setSplJSONList(splInfoArray);
                }
            }
        }
        return sysUsrVO;
    }

    @Override
    public Long add(SysUsrVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.info(logger, "add", "新增用户失败,用户对象为空.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        Long mainId = this.sysUsrApiService.addSysUsr(SysUsrVOTransform.toDTO(entity));
        //如果新增包含供应商用户则要加入供应商与用户关系
        String usrTypCd = entity.getUsrTypCd();
        String usrTypCdDic = PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_SYS_USR_TYP_CD_SPL);
        if (usrTypCdDic.equals(usrTypCd)) {
            entity.setId(mainId);
            entity.setUsrTypCd(usrTypCd);
            this.addSplUsrRInfo(entity);
        }
        return mainId;
    }

    @Override
    public Long addForSplUsr(SysUsrVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.info(logger, "add", "新增用户失败,用户对象为空.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }

        //先检查用户是否存在
        SysUsrQueryCondition sysUsrQueryCondition = new SysUsrQueryCondition();
        sysUsrQueryCondition.setUsrNm(entity.getUsrNm());
        sysUsrQueryCondition.setSysCd(CurrentThreadContext.getFlServiceSysCd());//系统代码
        List<SysUsrDTO> usrList = this.sysUsrApiService.searchSysUsrList(sysUsrQueryCondition);
        Long mainId = null;
        SysUsrDTO sysUsrDTO = SysUsrVOTransform.toDTO(entity);
        if (CollectionUtils.isEmpty(usrList)) {
            BaseLogUtils.info(logger, "add", "新增用户A.", CurrentThreadContext.getCurrentUserName());
            mainId = this.sysUsrApiService.addSysUsr(sysUsrDTO);
            //如果新增包含供应商用户则要加入供应商与用户关系
            String usrTypCd = entity.getUsrTypCd();
            String usrTypCdDic = PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_SYS_USR_TYP_CD_SPL);
            entity.setId(mainId);
            entity.setUsrTypCd(usrTypCdDic);
            this.addSplUsrRInfo(entity);
        } else {//更新
            BaseLogUtils.info(logger, "add", "新增用户B.", CurrentThreadContext.getCurrentUserName());
            SysUsrDTO newSysUsrDTO = usrList.get(0);
            newSysUsrDTO.setSplId(entity.getSplId());
            newSysUsrDTO.setSysUsrSysRDTO(sysUsrDTO.getSysUsrSysRDTO());
            newSysUsrDTO.setEmployeePosCd(entity.getEmployeePosCd());
            //Long rlId = newSysUsrDTO.getSysUsrSysRDTO().getId();
            //mainId = newSysUsrDTO.getId();
            //entity.setId(mainId);
            //entity.getSysUsrSysRVO().setId(rlId);
            this.modify(SysUsrVOTransform.toVO(newSysUsrDTO));
        }
        return mainId;
    }

    @Override
    public boolean modify(SysUsrVO entity) throws BaseException {
        if (null == entity || null == entity.getSysUsrSysRVO()) {
            BaseLogUtils.info(logger, "modify", "修改用户信息失败,用户对象为空.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        ;
        boolean pass = this.sysUsrApiService.modifySysUsr(SysUsrVOTransform.toDTO(entity));
        if (pass) {//修改成功后需要同时修改供应商与用户关系表数据
            this.addSplUsrRInfo(entity);
        }
        return pass;
    }

    /**
     * 新增供应商用户关系,有则修改,没有则新增
     *
     * @param entity
     */
    private void addSplUsrRInfo(SysUsrVO entity) {
        Long mainId = entity.getId();
        Long splId = entity.getSplId();
        Integer isSplChg = BaseConstant.IS_YESNO_NO;
        String employeePosCd = entity.getEmployeePosCd();
        if (null != employeePosCd && !"3".equals(employeePosCd)) {
            isSplChg = BaseConstant.IS_YESNO_YES;
        }
        String usrTypCd = entity.getUsrTypCd();
        String usrTypCdDic = PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_SYS_USR_TYP_CD_SPL);
        SplUsrRQC splUsrRQC = new SplUsrRQC();
        splUsrRQC.setUsrTypCd(usrTypCdDic);
        splUsrRQC.setUsrId(mainId);
        Long splOldId = entity.getSplOldId();//主要是修改的时候记录下老数据供应商ID,方便查出来修改
        if (StringUtils.isNotEmpty(splOldId)) {
            splUsrRQC.setSplId(splOldId);
        } else {
            splUsrRQC.setSplId(splId);
        }
        splUsrRQC.setIsDel(9);
        List<SplUsrRDTO> list = null;
        try {
            list = this.splInfoQueryApiService.searchSplUsrRList(splUsrRQC);
            if (CollectionUtils.isEmpty(list)) {//新增
                SplUsrRDTO splUsrRDTO = new SplUsrRDTO();
                splUsrRDTO.setUsrId(mainId);
                splUsrRDTO.setSplId(entity.getSplId());
                splUsrRDTO.setIsChg(isSplChg);
                splUsrRDTO.setIsSysDef(BaseConstant.IS_YESNO_NO);
                splUsrRDTO.setCrtTm(new Date());
                splUsrRDTO.setUsrTypCd(usrTypCdDic);
                splUsrRDTO.setIsDel(0);
                splUsrRDTO.setBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
                splUsrRDTO.setIsPrivate("0");
                splUsrRDTO.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
                splUsrRDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
                splUsrRDTO.setEmployeePosCd(employeePosCd);
                try {
                    this.splInfoApiService.addSplUsrR(splUsrRDTO);
                } catch (Exception e) {
                    BaseLogUtils.error(logger, "add", "新增供应商用户关系失败!", e);
                }
            } else {
                SplUsrRDTO splUsrRDTO = list.get(0);
                splUsrRDTO.setCrtTm(new Date());
                splUsrRDTO.setSplId(splId);
                splUsrRDTO.setIsChg(isSplChg);
                splUsrRDTO.setEmployeePosCd(employeePosCd);
                if (StringUtils.isNotEmpty(splUsrRDTO.getIsDel())) {
                    if (splUsrRDTO.getIsDel() == 1) {
                        splUsrRDTO.setIsDel(0);
                    }
                } else {
                    splUsrRDTO.setIsDel(0);
                }
                DataResultDTO modifySplUsrInfo = this.splInfoMofidyApiService.modifySplUsrR(splUsrRDTO);
                if (null == modifySplUsrInfo || !modifySplUsrInfo.getCode().equals(String.valueOf(BaseConstant.IS_YESNO_YES))) {
                    BaseLogUtils.error(logger, "modify", "修改供应商用户关系失败!" + JSON.toJSONString(splUsrRDTO));
                }
            }
        } catch (Exception e) {
            BaseLogUtils.error(logger, "modify", "获取供应商用户关系失败!" + JSON.toJSONString(splUsrRQC), e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        //经销商用户ID
        List<Long> splUsrIdList = null;
        //经销商用户关系List
        List<SplUsrRDTO> splUsrRDTOList = null ;
        SysUsrQueryCondition qc = (SysUsrQueryCondition) page.getCondition();
        if (null != qc && (StringUtils.isNotEmpty(qc.getSplId()) || StringUtils.isNotEmpty(qc.getSplNm()))) {
            SplUsrRQC splUsrRQC = new SplUsrRQC();
            if (StringUtils.isNotEmpty(qc.getSplId())) {
                splUsrRQC.setSplId(qc.getSplId());
            }
            if (StringUtils.isNotEmpty(qc.getSplNm())) {
                splUsrRQC.setSplNm(qc.getSplNm());
            }
            splUsrRQC.setIsDel(BaseConstant.IS_YESNO_NO);
            splUsrRQC.setUsrTypCd(MagicNumReplaceEnum.REP_NON_YESNO_VAL.getValStr());
            splUsrRDTOList = this.splUsrRApiService.searchSplUsrRList(splUsrRQC);
            if (CollectionUtils.isNotEmpty(splUsrRDTOList)) {
                BaseLogUtils.info(logger, "searchListPage", "获取用户列表,查询用户与经销商关系返回结果splUsrRDTOList:" + JSON.toJSONString(splUsrRDTOList));
                splUsrIdList = new ArrayList<>(splUsrRDTOList.size());
                for (SplUsrRDTO splUsrRDTO : splUsrRDTOList) {
                    if (StringUtils.isNotEmpty(splUsrRDTO.getUsrId())) {
                        splUsrIdList.add(splUsrRDTO.getUsrId());
                    }
                }
            }else{
                BaseLogUtils.info(logger, "searchListPage", "获取用户列表,查询用户与经销商关系返回空数据而结束！");
                return page ;
            }
            qc.setSplId(null);
            qc.setSplNm(null);
        }
        if (null != splUsrIdList) {
            qc.setSplUsrIdList(splUsrIdList);
        }
        page.setCondition(qc);
        BaseLogUtils.info(logger, "searchListPage", "获取用户列表,查询条件page:" + JSON.toJSONString(page));
        page = this.sysUsrApiService.searchSysUsrListPage(page);
        List<SysUsrDTO> sysUsrDTOList = (List<SysUsrDTO>) page.getResultList();
        BaseLogUtils.info(logger, "searchListPage", "获取用户列表A,查询结果page:" + JSON.toJSONString(page));
        List<Long> crtUsrIds = null ;
        if (CollectionUtils.isNotEmpty(sysUsrDTOList)) {
            //如果有供应商标识则要查询供应商名字
            if (null != qc && true == qc.getIsSplQuery()) {
                try {
                    BaseLogUtils.info(logger, "searchListPage", "获取用户列表B,qc:"+JSON.toJSONString(qc));
                    List<Long> usrIds = new ArrayList<>(sysUsrDTOList.size());
                    crtUsrIds = new ArrayList<>(sysUsrDTOList.size()) ;
                    for (SysUsrDTO sysUsrDTO : sysUsrDTOList) {
                        usrIds.add(sysUsrDTO.getId());
                        crtUsrIds.add(sysUsrDTO.getCrtUsrId());
                    }
                    String usrTypCdDic = PropertiesCacheUtils.getProperty(fl.constant.facade.framework.constant.DictionaryConstant.CODE_SYS_USR_TYP_CD_SPL);
                    SplUsrRQC splUsrRQC = new SplUsrRQC();
                    splUsrRQC.setUsrIdList(usrIds);
                    splUsrRQC.setUsrTypCd(usrTypCdDic);
                    splUsrRQC.setSplNm(qc.getSplNm());
                    splUsrRQC.setIsDel(BaseConstant.IS_YESNO_NO);
                    if (StringUtils.isNotEmpty(qc.getSplId())) {
                        splUsrRQC.setSplId(qc.getSplId());
                    }
                    List<SplUsrRDTO> splUsrList = null;
                    try {
                        splUsrList = this.splInfoQueryApiService.searchSplUsrRList(splUsrRQC);
                    } catch (Exception e) {
                        BaseLogUtils.error(logger, "searchListPage", "获取用户列表C", e);
                    }

                    BaseLogUtils.info(logger, "searchListPage", "获取用户列表D");
                    Map<Long, StringBuilder> usrIdAndSplNmMap = null ;//保存供应商名字
                    Map<Long, Integer> isSplChgMap = null ;//用户是否是管理员
                    Map<Long, String> employeePosCdgMap = null ;//用户是否是管理员
                    Map<Long, List<Long>> usrIdSplIdsMap = null ;//用户对应多个供应商ID
                    List<Long> splIdList = null ;
                    if (CollectionUtils.isNotEmpty(splUsrList)) {
                        //要查询供应商信息, 名称、省、市、事业部等
                        usrIdAndSplNmMap = new HashMap<>(splUsrList.size());//保存用户与供应商名字关系
                        isSplChgMap = new HashMap<>(splUsrList.size());//用户是否是管理员
                        employeePosCdgMap = new HashMap<>(splUsrList.size());//用户职位
                        usrIdSplIdsMap = new HashMap<>(splUsrList.size());//用户对应多个供应商ID
                        splIdList = new ArrayList<>(splUsrList.size());
                        StringBuilder splNmBuilder = null ;
                        for (SplUsrRDTO splUsrRDTO : splUsrList) {
                            Long usrId = splUsrRDTO.getUsrId();
                            String splNm = splUsrRDTO.getSplNm();
                            Long splId = splUsrRDTO.getSplId();
                            if(StringUtils.isNotEmpty(usrId)){
                                //用户与供应商名字关系
                                if(StringUtils.isNotEmpty(splNm)){
                                    if (usrIdAndSplNmMap.containsKey(usrId)) {
                                        splNmBuilder = usrIdAndSplNmMap.get(usrId);
                                        usrIdAndSplNmMap.put(usrId, splNmBuilder.append(";").append(splNm));
                                    } else {
                                        splNmBuilder = new StringBuilder(splNm);
                                        usrIdAndSplNmMap.put(usrId, splNmBuilder);
                                    }
                                }
                                //用户是否是管理员
                                if(StringUtils.isNotEmpty(splUsrRDTO.getIsChg())){
                                    isSplChgMap.put(usrId, splUsrRDTO.getIsChg());//如果需要此参数需要重新做整理,一个用户对应多个供应商的时候可能有的是管理员有的不是需要区分
                                }
                                //用户职位
                                if(StringUtils.isNotEmpty(splUsrRDTO.getEmployeePosCd())){
                                    employeePosCdgMap.put(usrId, splUsrRDTO.getEmployeePosCd()); //职位亦是如此，多经销商多职位
                                }

                                //用户对应多个供应商ID
                                if (usrIdSplIdsMap.containsKey(usrId)) {
                                    usrIdSplIdsMap.get(usrId).add(usrId);
                                } else {
                                    List<Long> tmpSplId = new ArrayList<>(MagicNumReplaceEnum.REP_DEFAULT_LEN_FOUR.getVal());
                                    tmpSplId.add(splId);
                                    usrIdSplIdsMap.put(usrId, tmpSplId);
                                }
                            }

                            //经销商ID List
                            if (!splIdList.contains(splId)) {
                                splIdList.add(splId);
                            }
                        }

                        String sysCd = CurrentThreadContext.getCurrentSysCd();
                        String orgCd = CurrentThreadContext.getCurrentOrgCd();

                        SplInfoQueryCondition splQC = new SplInfoQueryCondition();
                        splQC.setIds(splIdList);

                        List<SplInfoDTO> resultList = this.splInfoApiService.searchSplInfoList(splQC, sysCd, orgCd);

                        Map<Long, SplInfoDTO> splIdAndInfoMap = null ;
                        if (CollectionUtils.isNotEmpty(resultList)) {
                            splIdAndInfoMap = new HashMap<>(resultList.size());
                            for (SplInfoDTO splInfoDTO : resultList) {
                                splIdAndInfoMap.put(splInfoDTO.getId(), splInfoDTO);
                            }
                        }
                        //注册渠道
                        Map<String, String> regChlCdMap = this.sysDictionaryApiService.searchGroupToMap(WebBaseConstant.SYS_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, DictionaryConstant.CODE_SYS_DS_CD);
                        //经销商员工职位MAP
                        Map<String, String> employeePosMap = this.sysDictionaryApiService.searchGroupToMap(SplSystemConstant.SPL_SYS_CD, WebBaseConstant.SYS_P_ORG_CD, fl.spl.facade.framework.constant.DictionaryConstant.SPL_EMPLOYEE_POS_CD);

                        for (SysUsrDTO sysUsrDTO : sysUsrDTOList) {
                            sysUsrDTO.setRegChlCdNm(regChlCdMap.get(sysUsrDTO.getRegChlCd()));
                            sysUsrDTO.setSplNm(usrIdAndSplNmMap.containsKey(sysUsrDTO.getId()) ? usrIdAndSplNmMap.get(sysUsrDTO.getId()).toString() :"");
                            sysUsrDTO.setIsSplChg(isSplChgMap.get(sysUsrDTO.getId()));
                            sysUsrDTO.setEmployeePosCd(employeePosCdgMap.get(sysUsrDTO.getId()));
                            sysUsrDTO.setEmployeePosCdNm(employeePosMap.get(sysUsrDTO.getEmployeePosCd()));
                            //sysUsrDTO.setSplId(splIdMap.get(sysUsrDTO.getId()));
                            List<Long> splIds = usrIdSplIdsMap.get(sysUsrDTO.getId());
                            if (CollectionUtils.isNotEmpty(splIds)) {
                                StringBuffer splBuOrgCd = new StringBuffer("");
                                StringBuffer splProvinceNm = new StringBuffer("");
                                StringBuffer splCityNm = new StringBuffer("");
                                for (Long id : splIds) {
                                    SplInfoDTO tmp = null != splIdAndInfoMap ? splIdAndInfoMap.get(id) : null;
                                    if (tmp != null) {
                                        if (StringUtils.isNotEmpty(tmp.getBuOrgCdNms())) {
                                            splBuOrgCd.append(tmp.getBuOrgCdNms());
                                        }
                                        if (StringUtils.isNotEmpty(tmp.getRgnPrCdNm())) {
                                            splProvinceNm.append(tmp.getRgnPrCdNm());
                                        }
                                        if (StringUtils.isNotEmpty(tmp.getRgnCyCdNm())) {
                                            splCityNm.append(tmp.getRgnCyCdNm());
                                        }
                                    }
                                }
                                sysUsrDTO.setSplBuOrgCdNm(splBuOrgCd.toString());
                                sysUsrDTO.setSplProvinceNm(splProvinceNm.toString());
                                sysUsrDTO.setSplCityNm(splCityNm.toString());
                                splBuOrgCd.setLength(BaseConstant.IS_YESNO_NO);
                                splBuOrgCd = null;
                                splProvinceNm.setLength(BaseConstant.IS_YESNO_NO);
                                splProvinceNm = null;
                                splCityNm.setLength(BaseConstant.IS_YESNO_NO);
                                splCityNm = null;
                            }
                        }

                        usrIdAndSplNmMap.clear();
                        usrIdAndSplNmMap = null;

                        isSplChgMap.clear();
                        isSplChgMap = null;

                        employeePosCdgMap.clear();
                        employeePosCdgMap = null;

                        usrIdSplIdsMap.clear();
                        usrIdSplIdsMap = null;

                        splIdAndInfoMap.clear();
                        splIdAndInfoMap = null;
                    }
                } catch (Exception e) {
                    BaseLogUtils.error(logger, "searchListPage", "设置供应商名称失败!", e);
                }
            }

            BaseLogUtils.info(logger, "searchListPage", "获取用户列表E");

            //创建人ID-真实姓名 MAP
            Map<Long, String> crtUsrIdRlNmMap = null ;
            List<SysUsrVO> volist = SysUsrVOTransform.toVOList(sysUsrDTOList);
            if (CollectionUtils.isNotEmpty(volist)) {
                crtUsrIdRlNmMap = new HashMap<>(volist.size());
                if (CollectionUtils.isNotEmpty(crtUsrIds)) {
                    SysUsrQueryCondition condition = new SysUsrQueryCondition();
                    condition.setUsrIds(crtUsrIds);
                    condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
                    List<SysUsrDTO> listTmp = sysUsrApiService.searchSysUsrList(condition);
                    for (SysUsrDTO sysUsrDTO : listTmp) {
                        crtUsrIdRlNmMap.put(sysUsrDTO.getId(), sysUsrDTO.getRefRlNm());
                    }
                }
                for (SysUsrVO sysUsrVO : volist) {
                    sysUsrVO.setCrtUsrNm(crtUsrIdRlNmMap.get(sysUsrVO.getCrtUsrId()));
                }
                crtUsrIdRlNmMap.clear();
                crtUsrIdRlNmMap = null;
            }
            page.setResultList(volist);
        } else {
            page.setResultList(SysUsrVOTransform.toVOList(sysUsrDTOList));
        }
        return page;
    }

    @Override
    public List<KeyValueVO> searchIsValidList() throws BaseException {
        return this.getKvList(DictionaryConstant.CODE_GRP_IS_VALID_CD);
    }

    @Override
    public List<KeyValueVO> searchGenderCdList() throws BaseException {
        return this.getKvList(DictionaryConstant.CODE_GRP_GENDER_CD);
    }

    @Override
    public List<KeyValueVO> searchUsrTypCdList() throws BaseException {
        return this.getKvList(DictionaryConstant.CODE_GRP_USR_TYP_CD);
    }

    @Override
    public List<KeyValueVO> searchServiceSysCdList() throws BaseException {
        String attr1 = PropertiesCacheUtils.getProperty(WebBaseConstant.USER_MANAGE_SYSTEM_DIC_ATTR1_FLAG);
        return this.getKvList(DictionaryConstant.CODE_SYS_DS_CD, attr1, null);
    }

    /**
     * 根据字典中代码组值获取组员集合
     *
     * @param grpCd 代码组值
     * @return 集合
     */
    private List<KeyValueVO> getKvList(String grpCd) {
        if (StringUtils.isBlank(grpCd)) {
            return null;
        }
        List<KeyValueVO> kvList = new ArrayList<>();
        try {
            List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
            if (CollectionUtils.isNotEmpty(sysDictionaryDTOList)) {
                for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
                    KeyValueVO keyValueVO = new KeyValueVO();
                    keyValueVO.setKey(sysDictionaryDTO.getCode());
                    keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
                    kvList.add(keyValueVO);
                }
            }
        } catch (BaseException e) {
            BaseLogUtils.info(logger, "getKvList", "获取字典项组" + grpCd + "失败.ERROR:" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception e) {
            BaseLogUtils.error(logger, "getKvList", "获取字典项组" + grpCd + "失败,系统错误.ERROR:" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return kvList;
    }

    /**
     * 根据字典中代码组值获取组员集合
     *
     * @param grpCd 代码组值
     * @return 集合
     */
    private List<KeyValueVO> getKvList(String grpCd, String attr1, String attr2) {
        List<KeyValueVO> kvList = new ArrayList<>();
        try {
            if (StringUtils.isBlank(grpCd)) {
                return kvList;
            }
            List<KeyValueVO> kv = this.sysDictionaryApiService.getKvListWithAttr1AndAttr2(grpCd, attr1, attr2, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
            if (CollectionUtils.isNotEmpty(kv)) {
                kvList.addAll(kv);
            }
        } catch (BaseException e) {
            BaseLogUtils.info(logger, "getKvList", "获取字典项组" + grpCd + "、attr1:" + attr1 + "、attr2:" + attr2 + "失败.ERROR:" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception e) {
            BaseLogUtils.error(logger, "getKvList", "获取字典项组" + grpCd + "、attr1:" + attr1 + "、attr2:" + attr2 + "失败,系统错误.ERROR:" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return kvList;
    }

    @Override
    public boolean modifyPwdSysUsr(List<Long> ids, String pwd) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) {
            BaseLogUtils.info(logger, "modifyPwdSysUsr", "密码重置失败,主键参数为空.IDS:" + JSONObject.toJSONString(ids), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        if (StringUtils.isBlank(pwd)) {
            BaseLogUtils.info(logger, "modifyPwdSysUsr", "密码重置失败,密码为空.PWD:" + pwd, CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.sysUsrApiService.modifyPwdSysUsr(ids, pwd, CurrentThreadContext.getCurrentSysCd());
    }

    @Override
    public boolean modifyPwdSysUsr(Long id, String pwd, String sysCd, String mp) throws BaseException {
        boolean flag = this.sysUsrApiService.modifyPwdSysUsr(id, pwd, sysCd);
        if(flag){
            this.sendResetPwdMessage(mp);
        }
        return flag;
    }

    /**
     * 发送重置密码成功短信
     * @param mp
     */
    private void sendResetPwdMessage(String mp){
        SmsSendParamDTO dto = new SmsSendParamDTO();
        dto.setMp(mp);
        dto.setCode(210);
        dto.setParams(new String[]{});
        //短信
        dto.setType(0);
        try {
            DataResultDTO result = this.smsAddApiService.sendSMS(dto);
            BaseLogUtils.info(logger, "sendResetPwdMessage", mp + ",调用短信接口返回结果:" + JSON.toJSONString(result));
            if(result == null){
                BaseLogUtils.info(logger, "sendResetPwdMessage", mp + "发送短信异常，未返回结果");
            } else if(BaseConstant.IS_YESNO_YES_STR.equals(result.getCode())) {
                BaseLogUtils.info(logger, "sendResetPwdMessage", mp + "发送重置密码短信成功");
            }else {
                BaseLogUtils.error(logger, "sendResetPwdMessage", mp + "发送重置密码短信失败，" + result.getInfo());
            }
        }catch (Exception e){
            BaseLogUtils.error(logger, "sendResetPwdMessage", mp + "发送重置密码短信失败，error:" + e.getMessage());
        }
    }

    @Override
    public boolean login(String usrNm, String pwd, LoginType loginType) throws BaseException {
        boolean pass = false;
        if (StringUtils.isBlank(usrNm) || StringUtils.isBlank(pwd)) {
            return pass;
        }
        Subject user = SecurityUtils.getSubject();
//        UsernamePasswordToken token = new UsernamePasswordToken(usrNm, pwd);
//        token.setRememberMe(true);
        EasyTypeToken token=new EasyTypeToken(usrNm, pwd, loginType,true);

        //SecurityUtils.getSubject().login(new UsernamePasswordToken(usrNm, MD5Utils.getInstance().encrypt(pwd)));
        try {
            user.login(token);
            pass = true;
        } catch (AuthenticationException e) {
            BaseLogUtils.error(logger, "login", "其他错误！" + e.getMessage(), e);
            throw new BaseException(e.getMessage());
        } catch (BaseException e) {
            BaseLogUtils.error(logger, "login", "自定义错误！" + e.getMessage(), e);
            throw new BaseException(e.getMessage());
        } catch (Exception e) {
            BaseLogUtils.error(logger, "login", "登录失败.ERROR:" + e.getMessage(), CurrentThreadContext.getCurrentUserName());
            throw new BaseException("用户登录失败");
        } finally {
            token.clear();
        }
        return pass;
    }

    @Override
    public boolean searchUsrListByUsrNm(SysUsrQueryCondition condition)
            throws BaseException {
        if (null == condition || StringUtils.isEmpty(condition.getUsrNm()) || StringUtils.isEmpty(condition.getSysCd())) {
            return false;
        }

        return this.sysUsrApiService.searchSysUsrListByUsrNm(condition);
    }


    @SuppressWarnings("unchecked")
    @Override
    public Pager searchUsrByOrgIdRoleId(Pager page) throws BaseException {
        if (page == null) {
            BaseLogUtils.info(logger, "searchUsrByOrgIdRoleId", "根据功能权限关联关系分页查询人员列表失败,PAGE对象为空.PAGE:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        Pager pager = this.sysUsrApiService.searchUsrByOrgIdRoleId(page);
        List<SysUsrDTO> list = (List<SysUsrDTO>) pager.getResultList();
        pager.setResultList(SysUsrVOTransform.toVOList(list));
        return pager;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Pager searchUsrNotInOrgIdRoleId(Pager page) throws BaseException {
        if (page == null) {
            BaseLogUtils.info(logger, "searchUsrNotInOrgIdRoleId", "剔除功能权限关联关系分页查询人员列表失败,PAGE对象为空.PAGE:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        Pager pager = this.sysUsrApiService.searchUsrNotInOrgIdRoleId(page);
        List<SysUsrDTO> list = (List<SysUsrDTO>) pager.getResultList();
        pager.setResultList(SysUsrVOTransform.toVOList(list));
        return pager;
    }


    @Override
    public boolean modifyCurrentLoginUserInfo(Long roleId, Long orgId) throws BaseException {
        Boolean pass = false;
        if (StringUtils.isEmpty(roleId) || StringUtils.isEmpty(orgId)) {
            return pass;
        }

        UserDTO sysUsrDTO = (UserDTO)CurrentThreadContext.getValue(CurrentThreadContext.CURRENT_USER_INFO);
        if (null == sysUsrDTO) {
            return pass;
        }

        List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOList = sysUsrDTO.getSysUsrFunAuthRDTOList();
        if (CollectionUtils.isEmpty(sysUsrFunAuthRDTOList)) {
            return pass;
        }

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession(true);
        for (SysUsrFunAuthRDTO sysUsrFunAuthRDTO : sysUsrFunAuthRDTOList) {
            if (sysUsrFunAuthRDTO.getRoleId().equals(roleId) && sysUsrFunAuthRDTO.getOrgId().equals(orgId)) {
                pass = true;

                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_CD, sysUsrFunAuthRDTO.getOrgCd());
                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_ID, sysUsrFunAuthRDTO.getRoleId());
                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ROLE_NM, sysUsrFunAuthRDTO.getRoleNm());
                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_NM, sysUsrFunAuthRDTO.getOrgNm());

                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_ID, sysUsrFunAuthRDTO.getOrgId());
                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_SHT_NM, sysUsrFunAuthRDTO.getOrgShtNm());
                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_DISP_NM, sysUsrFunAuthRDTO.getOrgDispNm());
                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD, sysUsrFunAuthRDTO.getRgnDivCd());
                CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM, sysUsrFunAuthRDTO.getRgnDivCdNm());

                session.setAttribute(CurrentThreadContext.CURRENT_ORG_CD, sysUsrFunAuthRDTO.getOrgCd());
                session.setAttribute(CurrentThreadContext.CURRENT_ROLE_ID, sysUsrFunAuthRDTO.getRoleId());
                session.setAttribute(CurrentThreadContext.CURRENT_ROLE_NM, sysUsrFunAuthRDTO.getRoleNm());
                session.setAttribute(CurrentThreadContext.CURRENT_ORG_NM, sysUsrFunAuthRDTO.getOrgNm());
                session.setAttribute(CurrentThreadContext.CURRENT_ORG_ID, sysUsrFunAuthRDTO.getOrgId());
                session.setAttribute(CurrentThreadContext.CURRENT_ORG_SHT_NM, sysUsrFunAuthRDTO.getOrgShtNm());
                session.setAttribute(CurrentThreadContext.CURRENT_ORG_DISP_NM, sysUsrFunAuthRDTO.getOrgDispNm());
                session.setAttribute(CurrentThreadContext.CURRENT_ORG_RGN_CD, sysUsrFunAuthRDTO.getRgnDivCd());
                session.setAttribute(CurrentThreadContext.CURRENT_ORG_RGN_CD_NM, sysUsrFunAuthRDTO.getRgnDivCdNm());

                Boolean rtn = this.getResPowerCtrl();
                if (rtn) {
                    List<String> roleUrls = this.sysResPowerApiService.getSysResPowereListByRoleIds(sysUsrFunAuthRDTO.getRoleId(), CurrentThreadContext.getCurrentSysCd());
                    CurrentThreadContext.setValue(CurrentThreadContext.CURRENT_RES_POWER_URL, roleUrls);
                    session.setAttribute(CurrentThreadContext.CURRENT_RES_POWER_URL, roleUrls);
                }
                session.setAttribute(CurrentThreadContext.CURRENT_CHANGE_ROLE_FLAG, true);
                break;
            } else {
                session.setAttribute(CurrentThreadContext.CURRENT_CHANGE_ROLE_FLAG, false);
            }
        }
        return pass;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Pager searchUsrByOrgId(Pager page) throws BaseException {
        if (page == null) {
            BaseLogUtils.info(logger, "searchUsrByOrgId", "根据数据权限关联关系分页查询人员列表失败,PAGE对象为空.PAGE:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        Pager pager = this.sysUsrApiService.searchUsrByOrgId(page);
        List<SysUsrDTO> list = (List<SysUsrDTO>) pager.getResultList();
        pager.setResultList(SysUsrVOTransform.toVOList(list));
        return pager;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Pager searchUsrNotInOrgId(Pager page) throws BaseException {
        if (page == null) {
            BaseLogUtils.info(logger, "searchUsrNotInOrgId", "剔除数据权限关联关系分页查询人员列表失败,PAGE对象为空.PAGE:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        Pager pager = this.sysUsrApiService.searchUsrNotInOrgId(page);
        List<SysUsrDTO> list = (List<SysUsrDTO>) pager.getResultList();
        pager.setResultList(SysUsrVOTransform.toVOList(list));
        return pager;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Pager searchUsrForUpdate(Pager page) throws BaseException {
        if (page == null) {
            BaseLogUtils.info(logger, "searchUsrForUpdate", "修改数据权限人员列表失败,PAGE对象为空.PAGE:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        Pager pager = this.sysUsrApiService.searchUsrForUpdate(page);
        List<SysUsrDTO> list = (List<SysUsrDTO>) pager.getResultList();
        pager.setResultList(SysUsrVOTransform.toVOList(list));
        return pager;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Pager searchFunAuthUsrByOrgId(Pager page) throws BaseException {
        if (page == null) {
            page = new Pager();
        }

        SysUsrQueryCondition sysUsrQueryCondition = (SysUsrQueryCondition) page.getCondition();
        if (null == sysUsrQueryCondition || StringUtils.isEmpty(sysUsrQueryCondition.getSysCd())) {
            BaseLogUtils.info(logger, "searchFunAuthUsrByOrgId", "根据组织架构ID查询所有属于当前架构角色下所有的人员失败,系统代码为空.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "查询对象中系统代码不能为空!");

        }

        Pager pager = this.sysUsrApiService.searchFunAuthUsrByOrgId(page);
        List<SysUsrDTO> list = (List<SysUsrDTO>) pager.getResultList();
        if (CollectionUtils.isNotEmpty(list) && StringUtils.isNotEmpty(sysUsrQueryCondition.getOrgId())) {
            SysOrgDTO sysOrgDTO = sysOrgApiService.selectById(sysUsrQueryCondition.getOrgId());
            for (SysUsrDTO sysUsrDTO : list) {
                sysUsrDTO.setOrgCd(sysOrgDTO.getOrgCd());
            }
        }
        pager.setResultList(SysUsrVOTransform.toVOList(list));
        return pager;
    }

    @Override
    public Boolean checkUserInfoForModify(String usrNm, Long id) throws BaseException {
        if (StringUtils.isEmpty(usrNm)) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "验证用户名称不能为空!");
        }
        Boolean pass = true;
        SysUsrQueryCondition condition = new SysUsrQueryCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setUsrNm(usrNm);
        List<SysUsrDTO> list = this.sysUsrApiService.searchSysUsrList(condition);
        if (CollectionUtils.isEmpty(list)) {
            return pass;
        }

        for (SysUsrDTO sysUsrDTO : list) {
            Long idTmp = sysUsrDTO.getId();
            if (!id.equals(idTmp)) {
                pass = false;
                break;
            }
        }
        return pass;
    }

    /**
     * 不分页查询所有系统用户
     *
     * @param condition
     * @return
     * @author JiLi
     */
    @Override
    public List<SysUsrDTO> searchSysUsrList(SysUsrQueryCondition condition) throws BaseException {
        if (null == condition) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "查询对象不能为空!");
        }

        if (StringUtils.isEmpty(condition.getSysCd())) {
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "查询对象中系统代码不能为空!");
        }
        return this.sysUsrApiService.searchSysUsrList(condition);
    }

    @Override
    public boolean removeSplUsrRlInfo(Long usrId, Long splId, String usrTypCd) throws BaseException {
        return this.splInfoRemoveApiService.removeSplUsrRlInfo(usrId, splId, usrTypCd);
    }

    @Override
    public SysUsrDTO selectSysUsrWithAuthBy(String sysCd, String usrNm)
            throws BaseException {
        return this.sysUsrApiService.selectSysUsrWithAuthBy(sysCd, usrNm);
    }

    @Override
    public SysUsrVO selectByIdAndSplId(Long id, String sysCd, Long splId) {
        // TODO Auto-generated method stub
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(splId)) {
            return null;
        }
        SysUsrDTO dto = this.sysUsrApiService.selectSysUsrById(id, sysCd, CurrentThreadContext.getCurrentOrgCd());
        SysUsrVO sysUsrVO = SysUsrVOTransform.toVO(dto);

        if (null != sysUsrVO) {
            SplUsrRQC splUsrRQC = new SplUsrRQC();
            splUsrRQC.setSplId(splId);
            splUsrRQC.setUsrId(dto.getId());

            List<SplUsrRDTO> list = null;
            try {
                list = this.splInfoQueryApiService.searchSplUsrRList(splUsrRQC);
            } catch (Exception e) {
                BaseLogUtils.error(logger, "selectById", "获取供应商用户关系失败!" + JSON.toJSONString(splUsrRQC), e);
            }
            if (CollectionUtils.isNotEmpty(list)) {
                SplUsrRDTO splUsrRDTO = list.get(0);
                sysUsrVO.setSplId(splId);
                sysUsrVO.setIsSplChg(splUsrRDTO.getIsChg());
                sysUsrVO.setSplNm(splUsrRDTO.getSplNm());
                sysUsrVO.setEmployeePosCd(splUsrRDTO.getEmployeePosCd());
            }
        }
        return sysUsrVO;
    }

    /**
     * @MethodName getSysUsrByUsrId
     * @Description: 通过用户id获取用户基本信息
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2019/3/4 13:50
     * @Version: V1.0.0
     * @Param usrName
     * @Return cn.fl.system.vo.SysUsrVO
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Override
    public SysUsrDTO getSysUsrByUsrId(Long id) throws BaseException {
        BaseLogUtils.info(logger, "getSysUsrByName", "通过用户id获取用户基本信息开始");
        BaseLogUtils.info(logger, "getSysUsrByName", "id:" + id);
        SysUsrDTO usrDTO = null;
        if (StringUtils.isEmpty(id)) {
            BaseLogUtils.error(logger, "getSysUsrByName", "用户id不能为空");
            throw new BaseException("用户id不能为空！");
        }
        try {
            List<Long> ids = new ArrayList<>();
            ids.add(id);
            List<SysUsrDTO> usrDTOList = this.sysUsrApiService.selectSysUsrList(ids);
            if (CollectionUtils.isNotEmpty(usrDTOList)) {
                usrDTO = usrDTOList.get(0);
            }
        } catch (BaseException e) {
            BaseLogUtils.error(logger, "getSysUsrByName", "通过用户名获取用户基本信息异常" + e.getMessage(), e);
            throw new BaseException(e.getMessage());
        } catch (Exception e) {
            BaseLogUtils.error(logger, "getSysUsrByName", "通过用户名获取用户基本信息异常" + e.getMessage(), e);
            throw new BaseException(e.getMessage());
        }
        BaseLogUtils.info(logger, "getSysUsrByName", "通过用户名获取用户基本信息结束");
        return usrDTO;
    }

    @Override
    public SysUsrDTO getSysUsrByName(String usrName,String sysCd) throws BaseException {
        BaseLogUtils.info(logger, "getSysUsrByName", "通过用户名获取用户基本信息开始");
        BaseLogUtils.info(logger, "getSysUsrByName", "usrName:" + usrName);
        SysUsrDTO usrDTO = null;
        if (StringUtils.isEmpty(usrName)) {
            BaseLogUtils.error(logger, "getSysUsrByName", "用户名不能为空");
            throw new BaseException("用户名不能为空！");
        }
        try {
            DataResultDTO resultDTO =null;
            if(StringUtils.isEmpty(sysCd)) {
                resultDTO=this.sysUsrQueryApiService.getSysUsrDTOByUsrNm(null, usrName);
            }else{
                resultDTO=this.sysUsrQueryApiService.getSysUsrDTOByUsrNm(null,sysCd, usrName);
            }
            if (null != resultDTO && BaseConstant.IS_YESNO_YES == resultDTO.getSuccess()) {
                if (null != resultDTO.getData()) {
                    usrDTO = (SysUsrDTO) resultDTO.getData();
                }
            }
        } catch (BaseException e) {
            BaseLogUtils.error(logger, "getSysUsrByName", "通过用户名获取用户基本信息异常" + e.getMessage(), e);
            throw new BaseException(e.getMessage());
        } catch (Exception e) {
            BaseLogUtils.error(logger, "getSysUsrByName", "通过用户名获取用户基本信息异常" + e.getMessage(), e);
            throw new BaseException(e.getMessage());
        }
        BaseLogUtils.info(logger, "getSysUsrByName", "通过用户名获取用户基本信息结束");
        return usrDTO;
    }

    @Override
    public boolean modifySysUsrMp(String newMp) throws BaseException {
        BaseLogUtils.info(logger, "modifySysUsrMp", "修改用户手机号处理开始");
        boolean rtn = false;
        if (StringUtils.isEmpty(newMp)) {
            BaseLogUtils.error(logger, "modifySysUsrMp", "新手机号不能为空");
            throw new BaseException("新手机号不能为空！");
        }
        try {
            SysUsrDTO usrDTO = this.getSysUsrByUsrId(CurrentThreadContext.getCurrentUserId());
            if (null == usrDTO) {
                BaseLogUtils.error(logger, "modifySysUsrMp", "用户基本信息未获取到");
                throw new BaseException("用户基本信息未获取到！");
            }
            String oldMp = usrDTO.getMp();
            usrDTO.setRefRlNm(usrDTO.getUsrNm());

            SysUsrDTO modifyUsrDTO = new SysUsrDTO();
            modifyUsrDTO.setId(usrDTO.getId());
            modifyUsrDTO.setMp(newMp);
            modifyUsrDTO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
            modifyUsrDTO.setMdfTm(new Date());

            //添加修改记录
            this.sysUsrModifyApiService.addUsrMpUdtRecDto(usrDTO,oldMp, newMp );
            //修改用户手机号处理
            DataResultDTO resultDTO = this.sysUsrModifyApiService.modifySimData(null, modifyUsrDTO);
            if (null != resultDTO && BaseConstant.IS_YESNO_YES == resultDTO.getSuccess()) {
                BaseLogUtils.info(logger, "modifySysUsrMp", "修改用户手机号处理成功");
                rtn = true;
            }
        } catch (BaseException e) {
            BaseLogUtils.error(logger, "getSysUsrByName", "修改用户手机号异常" + e.getMessage(), e);
            throw new BaseException(e.getMessage());
        } catch (Exception e) {
            BaseLogUtils.error(logger, "getSysUsrByName", "修改用户手机号异常" + e.getMessage(), e);
            throw new BaseException(e.getMessage());
        }
        BaseLogUtils.info(logger, "modifySysUsrMp", "修改用户手机号处理结束");
        return rtn;
    }

    /**
     * @MethodName getAuthCd
     * @Description: 发生短信验证码信息
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2019/2/27 16:34
     * @Version: V1.0.0
     * @Param userName
     * @Param code
     * @Return cmm.mid.core.framework.dto.DataResultDTO
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Override
    public DataResultDTO getAuthCdByUsrNm(String userName, String code) throws BaseException {
        BaseLogUtils.info(logger, "getAuthCd", "获取验证码开始");

        if (StringUtils.isEmpty(userName)) {
            throw new BaseException("用户名不能为空");
        }
        SysUsrDTO sysUsrDTO = this.getSysUsrByName(userName,null);
        if (null == sysUsrDTO) {
            throw new BaseException("用户信息不存在");
        } else if (StringUtils.isEmpty(sysUsrDTO.getMp())) {
            throw new BaseException("手机号未维护，请联系管理员维护手机号信息");
        }
        DataResultDTO result = this.cmmSmsCdApiService.getAuthCd(sysUsrDTO.getMp(), code);
        BaseLogUtils.info(logger, "getAuthCd", "发送验证码结果：" + JSON.toJSONString(result));

        if (null != result && BaseConstant.IS_YESNO_YES == result.getSuccess()) {
           String mp=new StringBuffer(sysUsrDTO.getMp()).replace(3,7,"****").toString();
            result.setInfo("验证码已发送至" +mp );
        }
        BaseLogUtils.info(logger, "getAuthCd", "获取验证码结束");
        return result;
    }

    /**
     * @MethodName getAuthCd
     * @Description: 发生短信验证码信息
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2019/2/27 16:34
     * @Version: V1.0.0
     * @Param userName
     * @Param code
     * @Return cmm.mid.core.framework.dto.DataResultDTO
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Override
    public DataResultDTO getAuthCdByMp(String mp, String code) throws BaseException {
        BaseLogUtils.info(logger, "getAuthCd", "获取验证码开始");

        if (StringUtils.isEmpty(mp)) {
            throw new BaseException("手机号不能为空");
        }
        DataResultDTO result = this.cmmSmsCdApiService.getAuthCd(mp, code);
        BaseLogUtils.info(logger, "getAuthCd", "发送验证码结果：" + JSON.toJSONString(result));

        if (null != result && BaseConstant.IS_YESNO_YES == result.getSuccess()) {
            result.setInfo("验证码已发送至" + EncryptUtil.mobileEncrypt(mp));
        }
        BaseLogUtils.info(logger, "getAuthCd", "获取验证码结束");
        return result;
    }

    /**
     * @MethodName modifySysUsrPwd
     * @Description: 重置密码-校验身份信息
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2019/2/27 16:34
     * @Version: V1.0.0
     * @Param userName
     * @Param code
     * @Return cmm.mid.core.framework.dto.DataResultDTO
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Override
    public DataResultDTO verifyIdentity(String usrNm, String msgCode, String verifyCd,String usrMp,String certNo) throws BaseException {
        DataResultDTO result = new DataResultDTO(BaseConstant.IS_YESNO_YES, "校验成功");
        if (StringUtils.isEmpty(usrNm)) {
            throw new BaseException("用户名不能为空");
        }
        Long usrId=null;
        try {
            SysUsrDTO sysUsrDTO = this.getSysUsrByName(usrNm, CurrentThreadContext.getCurrentSysCd());
            if (null == sysUsrDTO) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("用户不存在或已被注销请联系管理员！");
                return result;
            }
            usrId=sysUsrDTO.getId();
            if (StringUtils.isEmpty(sysUsrDTO.getMp())) {
                throw new BaseException("手机号未维护，请联系管理员维护手机号信息");
            } else if (StringUtils.isEmpty(sysUsrDTO.getRefCertNo())) {
                throw new BaseException("证件号未维护，请联系管理员维护证件号信息");
            } else if (!usrMp.equals(sysUsrDTO.getMp())) {
                throw new BaseException("您输入的手机号与预留手机号不一致，请确认");
            } else if (!certNo.equals(sysUsrDTO.getRefCertNo())) {
                throw new BaseException("您输入的证件号与预留证件号不一致，请确认");
            }

            DataResultDTO resultDTO = this.cmmSmsCdApiService.getCheckCode(sysUsrDTO.getMp(), msgCode, verifyCd);
            if (null == resultDTO) {
                throw new BaseException("短信验证码验证失败");
            } else if (BaseConstant.IS_YESNO_NO == resultDTO.getSuccess()) {
                throw new BaseException(resultDTO.getInfo());
            }
            String key = "RES_PWD:" + usrId + System.currentTimeMillis();
            userRedisService.setex(key, 60*5, sysUsrDTO);
            Map<String, Object> map = new HashMap<>();
            map.put("cacheKey", key);
            map.put("usrId", usrId);
            result.setData(map);
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.error(logger, "verifyIdentity", "校验失败" + ex.getMessage(), ex);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("校验失败");
            BaseLogUtils.error(logger, "verifyIdentity", "校验失败" + ex.getMessage(), ex);
        }
        return result;
    }

    /**
     * @MethodName checkSysUsrPwd
     * @Description: 验证用户密码是否正确
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2019/12/23 15:12
     * @Version: V1.0.0
     * @Param usrNm
     * @Param pwd
     * @Return cmm.user.facade.system.dto.SysUsrDTO
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Override
    public SysUsrDTO checkSysUsrPwd(String usrNm,String pwd) throws BaseException{
        SysUsrDTO sysUsrDTO=null;
        Long usrId=null;
        try{
            BaseParamDTO baseParamDTO=new BaseParamDTO();
            baseParamDTO.setSysCd(WebBaseConstant.SYS_SYS_CD);
            baseParamDTO.setChannelCd(WebBaseConstant.SYS_WEB_CHANNEL_CD);
            DataResultDTO dataResultDTO=this.sysUsrQueryApiService.sysUsrLogin(baseParamDTO,WebBaseConstant.SYS_SYS_CD,usrNm,pwd);
            if(null == dataResultDTO){
                throw new BaseException("用户名或密码错误！");
            }

            sysUsrDTO=(SysUsrDTO)dataResultDTO.getData();
            if(null != sysUsrDTO){
                usrId=sysUsrDTO.getId();
            }
            if(StringUtils.isNotEmpty(usrId)){
                String key = CurrentThreadContext.getCurrentSysCd() + ":" + usrId + ":LOCK:RESPWD:TIMES";
                Object lockState=this.userRedisService.get(key+":LOCKSTATE");
                if(null != lockState && BaseConstant.IS_YESNO_YES_STR.equals(String.valueOf(lockState))){
                    usrId=null;
                    BaseLogUtils.info(logger, "sysUsrLogin", usrNm + ":用户被锁定一小时");
                    throw new BaseException("用户名密码输入错误超上限，请忘记密码找回或一小时后重试！");
                }
            }

            if(BaseConstant.IS_YESNO_NO == dataResultDTO.getSuccess()){
                throw new BaseException(dataResultDTO.getInfo());
            }
        }catch (BaseException e){
            BaseLogUtils.error(logger, "sysUsrLogin", usrNm + ":用户名或密码错误"+e.getMessage(),e);
            this.setUsrErrTimes(usrNm, usrId,"LOCK");
            throw new AuthenticationException(e.getMessage());
        }catch (Exception e){
            BaseLogUtils.error(logger, "sysUsrLogin", usrNm + ":用户名或密码错误"+e.getMessage(),e);
            throw new BaseException("网络连接异常");
        }
        return sysUsrDTO;
    }

    /**
     * @MethodName getResPowerCtrl
     * @Description: 获取菜单权限开关
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2019/5/7 15:03
     * @Version: V1.0.0
     * @Param
     * @Return java.lang.Boolean
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Override
    public Boolean getResPowerCtrl() throws BaseException {
        Boolean rtn = true;
        try {
            SysCtrlCfgQueryCondition qc = new SysCtrlCfgQueryCondition();
            qc.setSysCd(WebBaseConstant.SYS_SYS_CD);
            qc.setOrgCd(WebBaseConstant.SYS_P_ORG_CD);
            qc.setCtrlCode(SysResPowerConstant.SYS_RES_POWER_CTL);
            String code = sysCtrlCfgApiService.searchSysCtrlCfgByCtrlCode(qc);
            if (StringUtils.isNotEmpty(code) && BaseConstant.IS_YESNO_NO_STR.equals(code)) {
                rtn = false;
            }
        } catch (BaseException e) {
            BaseLogUtils.error(logger, "getResPowerCtrl", "获取开关异常，" + e.getMessage(), e);
        } catch (Exception e) {
            BaseLogUtils.error(logger, "getResPowerCtrl", "获取开关异常，" + e.getMessage(), e);
        }
        BaseLogUtils.info(logger, "getResPowerCtrl", "获取菜单权限开关:" + rtn);
        return rtn;
    }


   /**
    * @MethodName addSysUsrLoginOutInfo
    * @Description: 用户新增注销记录
    * @Author: wangpengtao@lionbridgecapital.cn
    * @CreateDate: 2020/1/19 15:19
    * @Version: V1.0.0
    * @Param sysUsrCauseCd
    * @Param operDetail
    * @Return java.lang.Long
    * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
    */
    @Override
    public Long addSysUsrLoginOutInfo(String sysUsrCauseCd,String operDetail) throws BaseException {
        BaseLogUtils.info(logger, "usrLogOutInfo", "用户注销日志处理开始");
        Long id=null;
        try {
            SysUsrLogoutLogDTO dto = new SysUsrLogoutLogDTO();
            dto.setUsrId(CurrentThreadContext.getCurrentUserId());
            dto.setFrontCd(CurrentThreadContext.getCurrentSysCd());
            dto.setOperCause(sysUsrCauseCd);
            dto.setOperDetail(operDetail);
            dto.setOperCd(1L);
            dto.setCrtTm(new Date());
            dto.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
            id = this.sysUsrAppAddApiService.addSysUsrLoginOutLogDTO(dto);
        } catch (BaseException e) {
            BaseLogUtils.info(logger, "usrLogOutInfo", "用户注销日志处理异常！", e);
            throw new BaseException(e.getMessage());
        } catch (Exception e) {
            BaseLogUtils.info(logger, "usrLogOutInfo", "用户注销日志处理异常！", e);
            throw new BaseException(e.getMessage());
        }
        BaseLogUtils.info(logger, "usrLogOutInfo", "用户注销日志处理结束 id:" + id);
        return id;
    }
    /**
     * @MethodName usrLogOutInfo
     * @Description: 用户系统注销
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2019/12/2 14:59
     * @Version: V1.0.0
     * @Param usrId
     * @Param sysCd
     * @Return boolean
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Override
    public boolean usrLogOutInfo(Long usrId,String sysCd) throws BaseException {
        BaseLogUtils.info(logger, "usrLogOutInfo", "用户注销处理开始");
        boolean rtn = false;

        if(StringUtils.isEmpty(usrId)){
            return rtn;
        }
        try {
            SysUsrSysRDTO sysUsrSysRDTO = new SysUsrSysRDTO();
            sysUsrSysRDTO.setUsrId(usrId);
            sysUsrSysRDTO.setSysCd(sysCd);
            sysUsrSysRDTO.setIsValid(BaseConstant.IS_YESNO_NO);
            sysUsrSysRDTO.setMdfUsrId(usrId);
            rtn = this.sysUsrApiService.modifyUsrR(sysUsrSysRDTO);
        } catch (BaseException e) {
            BaseLogUtils.info(logger, "usrLogOutInfo", "用户注销处理异常！", e);
            throw new BaseException(e.getMessage());
        } catch (Exception e) {
            BaseLogUtils.info(logger, "usrLogOutInfo", "用户注销处理异常！", e);
            throw new BaseException(e.getMessage());
        }
        BaseLogUtils.info(logger, "usrLogOutInfo", "用户注销处理结束" + rtn);
        return rtn;
    }


    /**
     * @MethodName usrLogOutInfo
     * @Description: 用户账号锁定
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2019/12/2 14:59
     * @Version: V1.0.0
     * @Param usrId
     * @Param sysCd
     * @Return boolean
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    @Override
    public boolean usrAccountLock(Long usrId,String sysCd) throws BaseException {
        BaseLogUtils.info(logger, "usrAccountLock", "用户账号锁定处理开始 usrId:"+usrId+",sysCd:"+sysCd);
        boolean rtn = false;

        try {
            if(StringUtils.isEmpty(usrId)){
                return rtn;
            }
            SysUsrSysRDTO sysUsrSysRDTO = new SysUsrSysRDTO();
            sysUsrSysRDTO.setUsrId(usrId);
            sysUsrSysRDTO.setSysCd(sysCd);
            sysUsrSysRDTO.setLockState(BaseConstant.IS_YESNO_YES);
            sysUsrSysRDTO.setMdfUsrId(usrId);
            rtn = this.sysUsrApiService.modifyUsrR(sysUsrSysRDTO);
        } catch (BaseException e) {
            BaseLogUtils.info(logger, "usrAccountLock", "用户账号锁定处理异常！", e);
            throw new BaseException(e.getMessage());
        } catch (Exception e) {
            BaseLogUtils.info(logger, "usrAccountLock", "用户账号锁定处理异常！", e);
            throw new BaseException(e.getMessage());
        }
        BaseLogUtils.info(logger, "usrAccountLock", "用户账号锁定处理结束" + rtn);
        return rtn;
    }

    @Override
    public ListPageVO<SysUsrVO> searchAuthSysUsrList(Pager page, SysUsrQueryCondition qc, String initTime, String orgId, String mgrType, String dataId, String dataTypCd) throws BaseException {
        if (null == page) page = new Pager();
        if (null == qc) qc = new SysUsrQueryCondition();
        // 角色配置
        SysCdMapDQueryCondition sysCdMapDQC = new SysCdMapDQueryCondition();
        sysCdMapDQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
        sysCdMapDQC.setOrgCd("0000");
        sysCdMapDQC.setmTypCd("M1079");
        List<String> getsCodeList = new ArrayList<>();
        if ("1".equals(mgrType)) {
            // 区域经理
            getsCodeList.add("1");
        } else {
            // 客户经理
            getsCodeList.add("2");
        }
        sysCdMapDQC.setsCodeList(getsCodeList);
        BaseLogUtils.newWebLogger("===查询默认角色规则Start===, sysCdMapDQC:" + JSON.toJSONString(sysCdMapDQC)).info();
        List<SysCdMapDDTO> sysCdMapDDTOList = this.sysCdMapApiService.searchSysCdMapDWithTargetList(sysCdMapDQC);
        BaseLogUtils.newWebLogger("===查询默认角色规则End===, sysCdMapDDTOList:" + JSON.toJSONString(sysCdMapDDTOList)).info();
        List<Long> roleIdList = new ArrayList<>();
        for (SysCdMapDDTO sysCdMapDDTO : sysCdMapDDTOList) {
            String roleId = sysCdMapDDTO.getTCode();
            if (StringUtils.isNotEmpty(roleId) && !roleIdList.contains(Long.valueOf(roleId))) {
                roleIdList.add(Long.valueOf(roleId));
            }
        }
        BaseLogUtils.newWebLogger("===角色id集合===, roleIdList:" + JSON.toJSONString(roleIdList)).info();

        if (CollectionUtils.isNotEmpty(roleIdList)) {
            qc.setRoleIdList(roleIdList);
        }

        List<Long> orgIds = new ArrayList<>();
        boolean flag = false;
        if (StringUtils.isNotEmpty(initTime) && "0".equals(initTime) && ("2".equals(mgrType) || "3".equals(mgrType)) && StringUtils.isNotEmpty(dataId)) {
            ExpBizManRecQC expBizManRecQC = new ExpBizManRecQC();
            expBizManRecQC.setDataId(Long.valueOf(dataId));
            expBizManRecQC.setDataTypCd(dataTypCd);
            expBizManRecQC.setAptTypCd("1");
            expBizManRecQC.setIsDel(BaseConstant.IS_YESNO_NO);
            BaseLogUtils.newWebLogger("===查询区域经理信息Start===, dataId:" + dataId).info();
            List<ExpBizManRecDTO> expBizManRecDTOS = this.expBizManRecApiService.searchList(expBizManRecQC);
            BaseLogUtils.newWebLogger("===查询区域经理信息End===, expBizManRecDTOS:" + JSON.toJSONString(expBizManRecDTOS)).info();
            if (CollectionUtils.isNotEmpty(expBizManRecDTOS) && expBizManRecDTOS.size() == 1) {
                ExpBizManRecDTO expBizManRecDTO = expBizManRecDTOS.get(0);
                Long usrIdArea = expBizManRecDTO.getUsrId();
                SysCdMapDQueryCondition sysCdMapDQCArea = new SysCdMapDQueryCondition();
                sysCdMapDQCArea.setSysCd(CurrentThreadContext.getCurrentSysCd());
                sysCdMapDQCArea.setOrgCd("0000");
                sysCdMapDQCArea.setmTypCd("M1079");
                List<String> getsCodeListArea = new ArrayList<>();
                getsCodeListArea.add("1");
                sysCdMapDQCArea.setsCodeList(getsCodeListArea);
                BaseLogUtils.newWebLogger("===查询区域经理角色规则Start===, sysCdMapDQCArea:" + JSON.toJSONString(sysCdMapDQCArea)).info();
                List<SysCdMapDDTO> sysCdMapDDTOListArea = this.sysCdMapApiService.searchSysCdMapDWithTargetList(sysCdMapDQCArea);
                BaseLogUtils.newWebLogger("===查询区域经理角色规则End===, sysCdMapDDTOListArea-size:" + (CollectionUtils.isNotEmpty(sysCdMapDDTOListArea)?sysCdMapDDTOListArea.size():0)).info();
                List<Long> roleIdListArea = new ArrayList<>();
                for (SysCdMapDDTO sysCdMapDDTO : sysCdMapDDTOListArea) {
                    String roleId = sysCdMapDDTO.getTCode();
                    if (StringUtils.isNotEmpty(roleId) && !roleIdListArea.contains(Long.valueOf(roleId))) {
                        roleIdListArea.add(Long.valueOf(roleId));
                    }
                }
                BaseLogUtils.newWebLogger("===区域经理角色id集合===, roleIdListArea:" + JSON.toJSONString(roleIdListArea)).info();
                SysUsrFunAuthRQueryCondition sysUsrFunAuthRQC = new SysUsrFunAuthRQueryCondition();
                sysUsrFunAuthRQC.setUsrId(usrIdArea);
                sysUsrFunAuthRQC.setIsValid(BaseConstant.IS_YESNO_YES);
                sysUsrFunAuthRQC.setSysCd("11000");
                sysUsrFunAuthRQC.setRoleIdList(roleIdListArea);
                List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS = this.priorityApiService.selectUsrOrgInfo(sysUsrFunAuthRQC);
                BaseLogUtils.newWebLogger("===区域经理相关信息集合===, sysUsrFunAuthRDTOS:" + JSON.toJSONString(sysUsrFunAuthRDTOS)).info();
                List<Long> orgIdList = new ArrayList<>();
                for (SysUsrFunAuthRDTO sysUsrFunAuthRDTO : sysUsrFunAuthRDTOS) {
                    String roleNm = sysUsrFunAuthRDTO.getRoleNm();
                    Long orgIdRes = sysUsrFunAuthRDTO.getOrgId();
                    if (StringUtils.isNotEmpty(roleNm) && roleNm.indexOf("区域经理") != -1 && StringUtils.isNotEmpty(orgIdRes) && !orgIdList.contains(orgIdList)) {
                        orgIdList.add(orgIdRes);
                    }
                }
                BaseLogUtils.newWebLogger("===区域经理组织架构信息===, orgIdList:" + JSON.toJSONString(orgIdList)).info();
                if (CollectionUtils.isNotEmpty(orgIdList)) {
                    for (Long orgIdRes : orgIdList) {
                        List<Long> orgIdQC = new ArrayList<>();
                        orgIdQC.add(Long.valueOf(orgIdRes));
                        List<SysOrgDTO> sysOrgDTOS = this.sysOrgApiService.searchRecuOrgWithchildBy(orgIdQC, "11000");
                        for (SysOrgDTO sysOrgDTO : sysOrgDTOS) {
                            if (!orgIds.contains(sysOrgDTO.getId())) {
                                orgIds.add(sysOrgDTO.getId());
                            }
                        }
                    }
                }
            }
            flag = true;
        }

        if ((!flag || CollectionUtils.isEmpty(orgIds)) && StringUtils.isNotEmpty(orgId)) {
            BaseLogUtils.newWebLogger("===查询默认角色规则Start===, sysCdMapDQC:" + JSON.toJSONString(sysCdMapDQC)).info();
            String[] arr = orgId.split(",");
            if (arr != null && arr.length > 1) {
                // orgId多个时批量查询，单个时单独查询
                for (int i = 0; i < arr.length; i++) {
                    orgIds.add(Long.valueOf(arr[i]));
                }
            } else {
                // 查询下级orgId
                List<Long> orgIdQC = new ArrayList<>();
                orgIdQC.add(Long.valueOf(orgId));
                List<SysOrgDTO> sysOrgDTOS = this.sysOrgApiService.searchRecuOrgWithchildBy(orgIdQC, "11000");
                for (SysOrgDTO sysOrgDTO : sysOrgDTOS) {
                    if (!orgIds.contains(sysOrgDTO.getId())) {
                        orgIds.add(sysOrgDTO.getId());
                    }
                }
            }

        }
        BaseLogUtils.newWebLogger("===组织架构id-list===sysUserQC:" + JSON.toJSONString(orgIds) + ",initTime:" + initTime).info();
        qc.setOrgIdList(orgIds);
        page.setCondition(qc);
        BaseLogUtils.newWebLogger("===根据条件查询系统用户Start===sysUserQC:" + JSON.toJSONString(qc) + ",initTime:" + initTime).info();
        Pager pager = this.searchFunAuthUsrByOrgId(page);
        BaseLogUtils.newWebLogger("===根据条件查询系统用户End===pager:" + JSON.toJSONString(pager)).info();
		List<SysUsrVO> sysUsrVOList = (List<SysUsrVO>) pager.getResultList();
		List<SysUsrVO> sysUsrVOS = new ArrayList<>();
		for (SysUsrVO sysUsrVO : sysUsrVOList) {
			SysUsrFunAuthRQueryCondition sysUsrFunAuthRQC = new SysUsrFunAuthRQueryCondition();
			sysUsrFunAuthRQC.setUsrId(sysUsrVO.getId());
			sysUsrFunAuthRQC.setIsValid(BaseConstant.IS_YESNO_YES);
			BaseLogUtils.newWebLogger("===根据条件查询系统用户角色Start===sysUsrFunAuthRQC1:" + JSON.toJSONString(sysUsrFunAuthRQC) + ",initTime:" + initTime).info();
			List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOList = this.sysUsrFunAuthRService.searchList(sysUsrFunAuthRQC);
			BaseLogUtils.newWebLogger("===根据条件查询系统用户角色End===sysUsrFunAuthRDTOList1:" + JSON.toJSONString(sysUsrFunAuthRDTOList) + ",initTime:" + initTime).info();
			for (SysUsrFunAuthRDTO sysUsrFunAuthRDTO : sysUsrFunAuthRDTOList) {
				Long orgIdRes = sysUsrFunAuthRDTO.getOrgId();
                Long roleId = sysUsrFunAuthRDTO.getRoleId();
                String roleNm = sysUsrFunAuthRDTO.getRoleNm();
                if (CollectionUtils.isNotEmpty(orgIds) && StringUtils.isNotEmpty(orgIdRes) && orgIds.contains(orgIdRes) && StringUtils.isNotEmpty(roleId) && roleIdList.contains(roleId)
                        && StringUtils.isNotEmpty(roleNm)) {
                    if ("1".equals(mgrType) && roleNm.indexOf("区域经理") != -1) {
                        sysUsrVO.setRoleNm(roleNm);
                    }
                    if (("2".equals(mgrType) || "3".equals(mgrType)) && roleNm.indexOf("客户经理") != -1) {
                        sysUsrVO.setRoleNm(roleNm);
                    }
                }
			}
            if (StringUtils.isEmpty(sysUsrVO.getRoleNm())) {
                if ("1".equals(mgrType)) {
                    sysUsrVO.setRoleNm("区域经理");
                } else {
                    // 客户经理
                    sysUsrVO.setRoleNm("客户经理");
                }
            }
			sysUsrVOS.add(sysUsrVO);
		}

		ListPageVO<SysUsrVO> listPage = new ListPageVO<>(sysUsrVOS, pager.getTotalCount());
		return listPage;
    }


    /**
     * 获取登录是否校验短息登录
     * @return
     */
    @Override
    public String getCtrlUsrLogin() throws BaseException{
        //0关闭 短息校验 1打开短息校验
        SysCtrlCfgQueryCondition qc = new SysCtrlCfgQueryCondition();
        qc.setSysCd(WebBaseConstant.SYS_SYS_CD);
        qc.setOrgCd(WebBaseConstant.SYS_P_ORG_CD);
        qc.setCtrlCode("CTRL_USR_0002");
        return this.sysCtrlCfgApiService.searchSysCtrlCfgByCtrlCode(qc);
    }

    /**
     * @MethodName sendNewPwdMsg
     * @Description: 发送新密码短信
     * @Author: wangpengtao@lionbridgecapital.cn
     * @CreateDate: 2019/8/20 14:55
     * @Version: V1.0.0
     * @Param mp
     * @Param newPwd
     * @Return void
     * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
     */
    private void sendNewPwdMsg(String mp, String newPwd) {
        BaseLogUtils.info(logger, "sendNewPwdMsg", "发送短信处理开始");
        BaseLogUtils.info(logger, "sendNewPwdMsg", mp + ":" + newPwd);

        try {
            SmsSendParamDTO dto = new SmsSendParamDTO();
            dto.setMp(mp);
            dto.setCode(SysUsrConstant.USR_MDY_USR_CODE);
            dto.setParams(new String[]{newPwd});
            dto.setType(0);//短信
            DataResultDTO dataResultDTO=this.smsAddApiService.sendSMS(dto);
            BaseLogUtils.info(logger, "sendNewPwdMsg", mp + ",调用短信接口返回结果" + JSON.toJSONString(dataResultDTO));
            if(null != dataResultDTO){
                if(BaseConstant.IS_YESNO_YES_STR.equals(dataResultDTO.getCode())){
                    BaseLogUtils.info(logger, "sendNewPwdMsg", mp + "发送密码修改结果短信成功");
                }else {
                    BaseLogUtils.error(logger, "sendNewPwdMsg", mp + "发送密码修改结果短信失败，"+dataResultDTO.getInfo());
                    throw new BaseException("短信发送失败，"+dataResultDTO.getInfo());
                }
            }else {
                BaseLogUtils.info(logger, "sendNewPwdMsg", mp + "发送短信异常，未返回结果");
                throw new BaseException("发送短信失败");
            }

        } catch (BaseException e) {
            throw new BaseException(e.getMessage());
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
        BaseLogUtils.info(logger, "sendNewPwdMsg", "发送短信处理结束");

    }

   /**
    * @MethodName setUsrState
    * @Description: 判断用户是否输入错误次数为5次，5次后锁定用户
    * @Author: wangpengtao@lionbridgecapital.cn
    * @CreateDate: 2019/12/24 19:13
    * @Version: V1.0.0
    * @Param usrNm
    * @Return void
    * @UpdateRemark [序号][YYYY-MM-DD] [更改人姓名][变更描述]
    */
    private int setUsrErrTimes(String usrNm,Long usrId,String keyName) throws BaseException {
        BaseLogUtils.info(logger, "setUsrState", "用户密码输入错误次数记录处理开始 usrNm:" + usrNm + ",userId:" + usrId + ",keyName:" + keyName);
        int errTimes = 0;
        try {
            if (StringUtils.isEmpty(usrId)) {
                return errTimes;
            }
            String key = CurrentThreadContext.getCurrentSysCd() + ":" + usrId + ":" + keyName + ":RESPWD:TIMES";

            BaseLogUtils.info(logger, "setUsrState", "用户密码输入错误次数记录处理开始 usrNm:" + usrNm + ",redisKey:" + key);

            Object objErrTimes = this.userRedisService.get(key);
            if (null == objErrTimes) {
                errTimes = 1;
                this.userRedisService.setex(key, 60 * 60 * 24, 1);
            } else {
                errTimes = (int) objErrTimes + 1;
                this.userRedisService.setex(key, 60 * 60 * 24, errTimes);
                BaseLogUtils.info(logger, "setUsrState", usrNm + "当前输入错误次数为" + errTimes);
                if ("ERROR".equals(keyName)) {
                    if (errTimes >= 5) {
                        BaseLogUtils.info(logger, "setUsrState", usrNm + ",输入错误次数已达到5次，注销当前用户信息");
                        BaseLogUtils.info(logger, "setUsrState", usrNm + ":设置为无效状态");
                        this.usrLogOutInfo(usrId, CurrentThreadContext.getCurrentSysCd());
                        this.userRedisService.setex(key, 60 * 60 * 24, 0);
                    }
                } else if ("LOCK".equals(keyName)) {
                    //当输入错误次数达到5次后锁定账户1小时
                    if (errTimes == 5){
                        BaseLogUtils.info(logger, "sysUsrLogin", usrNm + ":用户输入错误次数达到5次锁定一小时");
                        this.userRedisService.setex(key+":LOCKSTATE", 3600, BaseConstant.IS_YESNO_YES_STR);
                    }else if (errTimes >= 10) {
                        BaseLogUtils.info(logger, "setUsrState", usrNm + ",输入错误次数已达到10次，注销当前用户信息");
                        BaseLogUtils.info(logger, "setUsrState", usrNm + ":设置为锁定状态");
                        this.usrAccountLock(usrId, CurrentThreadContext.getCurrentSysCd());
                        this.userRedisService.setex(key, 60 * 60 * 24, 0);
                    }
                }
            }
        } catch (BaseException e) {
            BaseLogUtils.info(logger, "setUsrState", usrNm + ",输入错误次数记录异常" + e.getMessage(), e);
        } catch (Exception e) {
            BaseLogUtils.info(logger, "setUsrState", usrNm + ",输入错误次数记录异常" + e.getMessage(), e);
        }
        BaseLogUtils.info(logger, "setUsrState", "用户密码输入错误次数记录处理结束" + usrNm + "输入错误次数为" + errTimes);
        return errTimes;
    }

    @Override
    public boolean checkUsrRegDt(SysUsrDTO sysUsrDTO) throws BaseException{
        return this.sysUsrApiService.checkUsrIsValid(sysUsrDTO.getId(),sysUsrDTO.getUsrNm(),WebBaseConstant.SYS_SYS_CD);
    }

    @Override
    public Pager searchJCSplUsrListPage(Pager page) throws BaseException {
        if (page == null) {
            page = new Pager();
        }
        SysUsrQueryCondition sysUsrQueryCondition = (SysUsrQueryCondition) page.getCondition();
        if (null == sysUsrQueryCondition || StringUtils.isEmpty(sysUsrQueryCondition.getSplId())) {
            BaseLogUtils.info(logger, "searchJCSplUsrListPage", "根据组织架构ID查询所有属于当前架构角色下所有的人员失败,系统代码为空.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "查询对象中splId不能为空!");
        }
        SysOrgDTO sysOrgDTO = this.sysOrgApiService.selectBySysCdAndCompanyId(SplSystemConstant.JC_SPL_SYS_CD, String.valueOf(sysUsrQueryCondition.getSplId()));
        if(null == sysOrgDTO){
            BaseLogUtils.info(logger, "searchJCSplUsrListPage", "根据供应商ID获取组织机构为空", CurrentThreadContext.getCurrentUserName());
            return page;
        }
        sysUsrQueryCondition.setOrgId(sysOrgDTO.getId());
        page.setCondition(sysUsrQueryCondition);
        Pager pager = this.sysUsrApiService.searchUsrByOrgId(page);
        List<SysUsrDTO> list = (List<SysUsrDTO>) pager.getResultList();
        List<Long> usrIds = new ArrayList<>() ;
        List<Long> crtUsrIds = new ArrayList<>() ;
        if (CollectionUtils.isNotEmpty(list)) {
            for (SysUsrDTO sysUsrDTO : list) {
                usrIds.add(sysUsrDTO.getId());
                crtUsrIds.add(sysUsrDTO.getCrtUsrId());
                sysUsrDTO.setOrgCd(sysOrgDTO.getOrgCd());
                sysUsrDTO.setOrgCdNm(sysOrgDTO.getOrgNm());
                sysUsrDTO.setSplNm(sysOrgDTO.getOrgNm());
            }
        }
        List<SysUsrVO> sysUsrVOS = SysUsrVOTransform.toVOList(list);
        SysUsrFunAuthRQueryCondition condition = new SysUsrFunAuthRQueryCondition();
        condition.setOrgId(sysOrgDTO.getId());
        condition.setUsrIdList(usrIds);
        List<SysUsrFunAuthRDTO> dataList = this.priorityApiService.searchSysUsrFunAuthRDTOList(condition);
        if(CollectionUtils.isNotEmpty(dataList)){
            //创建人ID-真实姓名 MAP
            Map<Long, String> crtUsrIdRlNmMap = new HashMap<>() ;
            if (CollectionUtils.isNotEmpty(crtUsrIds)) {
                SysUsrQueryCondition usrQueryCondition = new SysUsrQueryCondition();
                sysUsrQueryCondition.setUsrIds(crtUsrIds);
                sysUsrQueryCondition.setSysCd(SplSystemConstant.FL_SYS_CD);
                List<SysUsrDTO> listTmp = sysUsrApiService.searchSysUsrList(sysUsrQueryCondition);
                if (CollectionUtils.isNotEmpty(listTmp)) {
                    for (SysUsrDTO sysUsrDTO : listTmp) {
                        crtUsrIdRlNmMap.put(sysUsrDTO.getId(), sysUsrDTO.getRefRlNm());
                    }
                }
            }
            Map<Long, SysDictionaryDTO> map = this.getRoleIdMap();
            for (SysUsrVO sysUsrVO : sysUsrVOS ) {
                sysUsrVO.setCrtUsrNm(crtUsrIdRlNmMap.get(sysUsrVO.getCrtUsrId()));
                sysUsrVO.setRegChlCdNm("pc后台");
                sysUsrVO.setRefIsValidNm("启用");
                if(BaseConstant.IS_YESNO_NO == sysUsrVO.getRefIsValid()){
                    sysUsrVO.setRefIsValidNm("禁用");
                }
                for (SysUsrFunAuthRDTO sysUsrFunAuthRDTO : dataList ) {
                    if(sysUsrFunAuthRDTO.getUsrId().equals(sysUsrVO.getId()) && null != map.get(sysUsrFunAuthRDTO.getRoleId())){
                        sysUsrVO.setEmployeePosCd(map.get(sysUsrFunAuthRDTO.getRoleId()).getCode());
                        sysUsrVO.setEmployeePosCdNm(map.get(sysUsrFunAuthRDTO.getRoleId()).getCodeNm());
                    }
                }
            }
        }
        pager.setResultList(sysUsrVOS);
        return pager;
    }

    @Override
    public SysUsrVO selectJCSplUsrByIdAndSplId(Long usrId, String splId) throws BaseException {
        if (StringUtils.isEmpty(usrId) || StringUtils.isEmpty(splId)) {
            return null;
        }
        SysUsrDTO dto = this.sysUsrApiService.selectSysUsrById(usrId, SplSystemConstant.JC_SPL_SYS_CD, SplSystemConstant.DEFAULT_ORG_CD);
        SysUsrVO sysUsrVO = SysUsrVOTransform.toVO(dto);
        if (null != sysUsrVO) {
            SysOrgDTO sysOrgDTO = this.sysOrgApiService.selectBySysCdAndCompanyId(SplSystemConstant.JC_SPL_SYS_CD,splId);
            if(null != sysOrgDTO){
               sysUsrVO.setSplId(Long.valueOf(splId));
               sysUsrVO.setSplNm(sysOrgDTO.getOrgNm());
               sysUsrVO.setRegChlCdNm("pc后台");
               sysUsrVO.setRefIsValidNm("启用");
               if(BaseConstant.IS_YESNO_NO == sysUsrVO.getRefIsValid()){
                   sysUsrVO.setRefIsValidNm("禁用");
               }
               SysUsrFunAuthRQueryCondition sysUsrFunAuthRQC = new SysUsrFunAuthRQueryCondition();
               sysUsrFunAuthRQC.setUsrId(sysUsrVO.getId());
               sysUsrFunAuthRQC.setOrgId(sysOrgDTO.getId());
               sysUsrFunAuthRQC.setIsValid(BaseConstant.IS_YESNO_YES);
               List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOList = this.priorityApiService.searchSysUsrFunAuthRDTOList(sysUsrFunAuthRQC);
               Long roleId = sysUsrFunAuthRDTOList.get(0).getRoleId();
               Map<Long, SysDictionaryDTO> map = this.getRoleIdMap();
               sysUsrVO.setEmployeePosCd(map.get(roleId).getCode());
               sysUsrVO.setEmployeePosCdNm(map.get(roleId).getCodeNm());
           }
        }
        return sysUsrVO;
    }

    @Override
    public Long addJCSplUsr(SysUsrVO entity) throws BaseException {
        if (null == entity || StringUtils.isEmpty(entity.getSplId()) || StringUtils.isEmpty(entity.getEmployeePosCd())) {
            BaseLogUtils.info(logger, "add", "新增用户失败,用户对象为空.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增用户失败,用户对象为空");
        }

        Long roleId = this.getRoleByDic(entity.getEmployeePosCd());
        Integer isChg = BaseConstant.IS_YESNO_YES;
        if("3".equals(entity.getEmployeePosCd())){
            isChg = BaseConstant.IS_YESNO_NO;
        }
        if (StringUtils.isEmpty(roleId)) {
            BaseLogUtils.info(logger, "add", "新增用户失败,员工职位为空.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增用户失败,员工职位为空");
        }

        //先检查供应商组织架构是否维护
        SysOrgDTO sysOrgDTO = this.sysOrgApiService.selectBySysCdAndCompanyId(SplSystemConstant.JC_SPL_SYS_CD, String.valueOf(entity.getSplId()));
        if(null == sysOrgDTO){
            sysOrgDTO = new SysOrgDTO();
            sysOrgDTO.setSysCd(SplSystemConstant.JC_SPL_SYS_CD);
            sysOrgDTO.setCompanyId(String.valueOf(entity.getSplId()));
            sysOrgDTO.setOrgNm(entity.getSplNm());
            sysOrgDTO.setCrtUsrId(entity.getCrtUsrId());
            sysOrgDTO.setOrgPid(0L);
            Long orgId = this.sysOrgApiService.addSysOrg(sysOrgDTO);
            sysOrgDTO.setId(orgId);
        }
        //先检查用户是否存在
        SysUsrQueryCondition sysUsrQueryCondition = new SysUsrQueryCondition();
        sysUsrQueryCondition.setUsrNm(entity.getUsrNm());
        sysUsrQueryCondition.setSysCd(SplSystemConstant.JC_SPL_SYS_CD);//系统代码
        List<SysUsrDTO> usrList = this.sysUsrApiService.searchSysUsrList(sysUsrQueryCondition);
        Long mainId = null;
        SysUsrDTO sysUsrDTO = SysUsrVOTransform.toDTO(entity);
        if (CollectionUtils.isEmpty(usrList)) {
            BaseLogUtils.info(logger, "add", "新增用户.", CurrentThreadContext.getCurrentUserName());
            mainId = this.sysUsrApiService.addSysUsr(sysUsrDTO);
        }else{
            mainId = usrList.get(0).getId();
        }
        //用户数据权限表
        boolean success = false;
        SysUsrDatAuthRQueryCondition sysUsrDatAuthRQC = new SysUsrDatAuthRQueryCondition();
        sysUsrDatAuthRQC.setUsrId(mainId);
        sysUsrDatAuthRQC.setOrgId(sysOrgDTO.getId());
        List<SysUsrDatAuthRDTO> sysUsrDatAuthRDTOList = this.priorityApiService.searchSysUsrDatAuthRDTOList(sysUsrDatAuthRQC);
        if(CollectionUtils.isNotEmpty(sysUsrDatAuthRDTOList)){
            List<Long> usrIdList = new ArrayList<>();
            usrIdList.add(mainId);
            success = this.priorityApiService.removeSysUsrDatAuthR(sysOrgDTO.getId(),usrIdList);
            if(!success){
                BaseLogUtils.info(logger, "add", "新增用户失败,维护关系表失败.", CurrentThreadContext.getCurrentUserName());
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增用户失败");
            }
        }
        List<SysUsrDatAuthRDTO> sysUsrDatAuthRDTOS = new ArrayList<SysUsrDatAuthRDTO>();
        SysUsrDatAuthRDTO sysUsrDatAuthRDTO = new SysUsrDatAuthRDTO();
        sysUsrDatAuthRDTO.setOrgId(sysOrgDTO.getId());
        sysUsrDatAuthRDTO.setIsChg(isChg);
        sysUsrDatAuthRDTO.setUsrId(mainId);
        sysUsrDatAuthRDTOS.add(sysUsrDatAuthRDTO);
        success = this.priorityApiService.addSysUsrDatAuthR(sysUsrDatAuthRDTOS);
        if(!success){
            BaseLogUtils.info(logger, "add", "新增用户失败,维护关系表失败.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增用户失败");
        }
        //用户权限表
        SysUsrFunAuthRQueryCondition sysUsrFunAuthRQC = new SysUsrFunAuthRQueryCondition();
        sysUsrFunAuthRQC.setUsrId(mainId);
        sysUsrFunAuthRQC.setOrgId(sysOrgDTO.getId());
        sysUsrFunAuthRQC.setIsValid(BaseConstant.IS_YESNO_YES);
        List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOList = this.priorityApiService.searchSysUsrFunAuthRDTOList(sysUsrFunAuthRQC);
        if(CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOList)){
            BaseLogUtils.info(logger, "add", "新增用户失败,已有该员工.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增用户失败,已有该员工");
        }

        List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS = new ArrayList<SysUsrFunAuthRDTO>();
        SysUsrFunAuthRDTO sysUsrFunAuthRDTO = new SysUsrFunAuthRDTO();
        sysUsrFunAuthRDTO.setOrgId(sysOrgDTO.getId());
        sysUsrFunAuthRDTO.setRoleId(roleId);
        sysUsrFunAuthRDTO.setIsChg(isChg);
        sysUsrFunAuthRDTO.setUsrId(mainId);
        sysUsrFunAuthRDTOS.add(sysUsrFunAuthRDTO);
        success = this.priorityApiService.addSysUsrFunAuthR(sysUsrFunAuthRDTOS);
        if(!success){
            BaseLogUtils.info(logger, "add", "新增用户失败,维护关系表失败.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "新增用户失败");
        }

        return mainId;
    }

    @Override
    public boolean modifyJCSplUsr(SysUsrVO entity) throws BaseException {
        if (null == entity || StringUtils.isEmpty(entity.getId()) || StringUtils.isEmpty(entity.getSplId()) || StringUtils.isEmpty(entity.getEmployeePosCd()) || StringUtils.isEmpty(entity.getSysUsrSysRVO().getIsValid())) {
            BaseLogUtils.info(logger, "modify", "修改用户信息失败,用户对象为空.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        };

        Long roleId = this.getRoleByDic(entity.getEmployeePosCd());
        Integer isChg = BaseConstant.IS_YESNO_YES;
        if("3".equals(entity.getEmployeePosCd())){
            isChg = BaseConstant.IS_YESNO_NO;
        }
        if (StringUtils.isEmpty(roleId)) {
            BaseLogUtils.info(logger, "add", "修改用户失败,员工职位为空.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改用户失败,员工职位为空");
        }

        //先检查供应商组织架构是否维护
        SysOrgDTO sysOrgDTO = this.sysOrgApiService.selectBySysCdAndCompanyId(SplSystemConstant.JC_SPL_SYS_CD, String.valueOf(entity.getSplId()));
        if(null == sysOrgDTO){
            BaseLogUtils.info(logger, "add", "修改用户失败,供应商对应组织架构为空.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改用户失败,供应商对应组织架构为空");
        }
        //先检查用户是否存在
        SysUsrDTO sysUsrDTO = this.sysUsrApiService.selectSysUsrById(entity.getId(), SplSystemConstant.JC_SPL_SYS_CD, SplSystemConstant.DEFAULT_ORG_CD);
        if (null == sysUsrDTO) {
            BaseLogUtils.info(logger, "add", "修改用户失败,用户不存在.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改用户失败,用户不存在");
        }
        boolean pass = false;
        if(!sysUsrDTO.getIsValid().equals(entity.getIsValid())){
            pass = this.sysUsrApiService.modifySysUsr(SysUsrVOTransform.toDTO(entity));
            if (!pass) {//修改成功后需要同时修改供应商与用户关系表数据
                BaseLogUtils.info(logger, "add", "修改用户失败.", CurrentThreadContext.getCurrentUserName());
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改用户失败");
            }
        }

        //用户数据权限表
        SysUsrDatAuthRQueryCondition sysUsrDatAuthRQC = new SysUsrDatAuthRQueryCondition();
        sysUsrDatAuthRQC.setUsrId(entity.getId());
        sysUsrDatAuthRQC.setOrgId(sysOrgDTO.getId());
        List<SysUsrDatAuthRDTO> sysUsrDatAuthRDTOList = this.priorityApiService.searchSysUsrDatAuthRDTOList(sysUsrDatAuthRQC);
        if(CollectionUtils.isNotEmpty(sysUsrDatAuthRDTOList)){
            List<Long> usrIdList = new ArrayList<>();
            usrIdList.add(entity.getId());
            pass = this.priorityApiService.removeSysUsrDatAuthR(sysOrgDTO.getId(),usrIdList);
            if(!pass){
                BaseLogUtils.info(logger, "add", "修改用户失败,维护关系表失败.", CurrentThreadContext.getCurrentUserName());
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改用户失败");
            }
        }
        List<SysUsrDatAuthRDTO> sysUsrDatAuthRDTOS = new ArrayList<SysUsrDatAuthRDTO>();
        SysUsrDatAuthRDTO sysUsrDatAuthRDTO = new SysUsrDatAuthRDTO();
        sysUsrDatAuthRDTO.setOrgId(sysOrgDTO.getId());
        sysUsrDatAuthRDTO.setIsChg(isChg);
        sysUsrDatAuthRDTO.setUsrId(entity.getId());
        sysUsrDatAuthRDTOS.add(sysUsrDatAuthRDTO);
        pass = this.priorityApiService.addSysUsrDatAuthR(sysUsrDatAuthRDTOS);
        if(!pass){
            BaseLogUtils.info(logger, "add", "修改用户失败,维护关系表失败.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改用户失败");
        }

        //用户权限表
        SysUsrFunAuthRQueryCondition sysUsrFunAuthRQC = new SysUsrFunAuthRQueryCondition();
        sysUsrFunAuthRQC.setUsrId(entity.getId());
        sysUsrFunAuthRQC.setOrgId(sysOrgDTO.getId());
        sysUsrFunAuthRQC.setIsValid(BaseConstant.IS_YESNO_YES);
        List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOList = this.priorityApiService.searchSysUsrFunAuthRDTOList(sysUsrFunAuthRQC);
        if(CollectionUtils.isNotEmpty(sysUsrFunAuthRDTOList)){
            List<Long> usrIdList = new ArrayList<>();
            usrIdList.add(entity.getId());
            for (SysUsrFunAuthRDTO usrFunAuthRDTO : sysUsrFunAuthRDTOList) {
                pass = this.priorityApiService.removeSysUsrFunAuthRUsr(usrFunAuthRDTO.getOrgId(),usrFunAuthRDTO.getRoleId(),usrIdList);
                if(!pass){
                    BaseLogUtils.info(logger, "add", "修改用户失败,维护关系表失败.", CurrentThreadContext.getCurrentUserName());
                    throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改用户失败");
                }
            }
        }
        List<SysUsrFunAuthRDTO> sysUsrFunAuthRDTOS = new ArrayList<SysUsrFunAuthRDTO>();
        SysUsrFunAuthRDTO sysUsrFunAuthRDTO = new SysUsrFunAuthRDTO();
        sysUsrFunAuthRDTO.setOrgId(sysOrgDTO.getId());
        sysUsrFunAuthRDTO.setRoleId(roleId);
        sysUsrFunAuthRDTO.setUsrId(entity.getId());
        sysUsrFunAuthRDTO.setIsChg(isChg);
        sysUsrFunAuthRDTOS.add(sysUsrFunAuthRDTO);
        pass = this.priorityApiService.addSysUsrFunAuthR(sysUsrFunAuthRDTOS);
        if(!pass){
            BaseLogUtils.info(logger, "add", "修改用户失败,维护关系表失败.", CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改用户失败");
        }
        return pass;
    }

    @Override
    public boolean removeJCSplUsrRlInfo(List<Long> usrIds, String splId, String employeePosCd) throws BaseException {
        if(CollectionUtils.isEmpty(usrIds)){
            throw new BaseException("用户ID不能为空!");
        }
        if(StringUtils.isEmpty(splId)){
            throw new BaseException("供应商ID不能为空!");
        }
        if(StringUtils.isEmpty(employeePosCd)){
            throw new BaseException("员工职位不能为空!");
        }
        boolean success = false;
        Long roleId = getRoleByDic(employeePosCd);
        if(StringUtils.isNotEmpty(roleId)){
            SysOrgDTO sysOrgDTO = this.sysOrgApiService.selectBySysCdAndCompanyId(SplSystemConstant.JC_SPL_SYS_CD,splId);
            //删除组织架构 角色 用户关系
            if(null != sysOrgDTO){
                success = this.priorityApiService.removeSysUsrDatAuthR(sysOrgDTO.getId(), usrIds);
                if(success){
                    success = this.priorityApiService.removeSysUsrFunAuthRUsr(sysOrgDTO.getId(), roleId, usrIds);
                    return success;
                }
            }
        }
        return success;
    }

    /**
     * 根据员工职位获取角色ID
     * @param employeePosCd
     * @return
     */
    private Long getRoleByDic(String employeePosCd){
        Long roleId = null;
        if(StringUtils.isNotEmpty(employeePosCd)){
            List<SysDictionaryDTO> sysDictionaryDTOS = this.sysDictionaryApiService.searchGroup(SplSystemConstant.JC_SPL_SYS_CD,SplSystemConstant.DEFAULT_ORG_CD,SplSystemConstant.SPL_EMPLOYEE_POS_CD);
            if(CollectionUtils.isNotEmpty(sysDictionaryDTOS)){
                for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOS ) {
                    if(employeePosCd.equals(sysDictionaryDTO.getCode())){
                        if(StringUtils.isNotEmpty(sysDictionaryDTO.getCodeAttr1())){
                            roleId = Long.valueOf(sysDictionaryDTO.getCodeAttr1());
                            break;
                        }
                    }
                }
            }
        }
        return roleId;
    }

    /**
     * 获取已code为key的员工职位MAP
     * @return
     */
    private Map<String, SysDictionaryDTO> getEmployeePosCdMap(){
        Map<String, SysDictionaryDTO> map = new HashMap<>();
        List<SysDictionaryDTO> sysDictionaryDTOS = this.sysDictionaryApiService.searchGroup(SplSystemConstant.JC_SPL_SYS_CD,SplSystemConstant.DEFAULT_ORG_CD,SplSystemConstant.SPL_EMPLOYEE_POS_CD);
        if(CollectionUtils.isNotEmpty(sysDictionaryDTOS)){
            for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOS ) {
                    map.put(sysDictionaryDTO.getCode(),sysDictionaryDTO);
                }
            }
        return map;
    }


    /**
     * 获取已CodeAttr1为key的员工职位MAP
     * @return
     */
    private Map<Long, SysDictionaryDTO> getRoleIdMap(){
        Map<Long, SysDictionaryDTO> map = new HashMap<>();
        List<SysDictionaryDTO> sysDictionaryDTOS = this.sysDictionaryApiService.searchGroup(SplSystemConstant.JC_SPL_SYS_CD,SplSystemConstant.DEFAULT_ORG_CD,SplSystemConstant.SPL_EMPLOYEE_POS_CD);
        if(CollectionUtils.isNotEmpty(sysDictionaryDTOS)){
            for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOS ) {
                if(StringUtils.isNotEmpty(sysDictionaryDTO.getCodeAttr1())){
                    map.put(Long.valueOf(sysDictionaryDTO.getCodeAttr1()),sysDictionaryDTO);
                }
            }
        }
        return map;
    }

    /**
     * 获取已CodeAttr1为key的员工职位MAP
     * @return
     */
    private List<Long> getRoleIdList(){
        List<Long> roleIdList = new ArrayList<>();
        List<SysDictionaryDTO> sysDictionaryDTOS = this.sysDictionaryApiService.searchGroup(SplSystemConstant.JC_SPL_SYS_CD,SplSystemConstant.DEFAULT_ORG_CD,SplSystemConstant.SPL_EMPLOYEE_POS_CD);
        if(CollectionUtils.isNotEmpty(sysDictionaryDTOS)){
            for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOS ) {
                if(StringUtils.isNotEmpty(sysDictionaryDTO.getCodeAttr1())){
                    roleIdList.add(Long.valueOf(sysDictionaryDTO.getCodeAttr1()));
                }
            }
        }
        return roleIdList;
    }

    @Override
    public boolean modifyPwdJCSysUsr(List<Long> ids, String pwd) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) {
            BaseLogUtils.info(logger, "modifyPwdSysUsr", "密码重置失败,主键参数为空.IDS:" + JSONObject.toJSONString(ids), CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        if (StringUtils.isBlank(pwd)) {
            BaseLogUtils.info(logger, "modifyPwdSysUsr", "密码重置失败,密码为空.PWD:" + pwd, CurrentThreadContext.getCurrentUserName());
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
        }
        return this.sysUsrApiService.modifyPwdSysUsr(ids, pwd, SplSystemConstant.JC_SPL_SYS_CD);
    }

}
